#include "Timer.h"
#include "TimeManager.h"
#include <cassert>

namespace sol9
{
namespace util
{

	Timer::Timer(bool bImmediately)
		: m_nTime(0), m_bWorked(bImmediately)
	{
		TimeManager::s_listTimers.push_back(this);
	}

	Timer::Timer(u32 nTime)
		: m_nTime(nTime), m_bWorked(false)
	{
		TimeManager::s_listTimers.push_back(this);
	}

	Timer::~Timer()
	{
		kill();
	}

	void Timer::begin(bool bReset)
	{
		assert(!m_bWorked && "<Timer> this timer is already running!");

		if (bReset)
			reset();

		m_bWorked = true;
	}

	void Timer::end()
	{
		assert(m_bWorked && "<Timer> this timer were already stop!");
			
		m_bWorked = false;
	}

	void Timer::reset()
	{
		m_nTime = 0;
		m_bWorked = false;
	}

	void Timer::kill()
	{
		TimeManager::s_listTimers.remove(this);
	}

	Timer Timer::operator+(const Timer& other) const
	{
		Timer out;
		out.m_nTime = this->m_nTime + other.m_nTime;
		return out;
	}

	Timer Timer::operator+(const u32 time) const
	{
		Timer out;
		out.m_nTime = this->m_nTime + time;
		return out;
	}

	Timer& Timer::operator+=(const Timer& other)
	{
		this->m_nTime += other.m_nTime;
		return *this;
	}

	Timer& Timer::operator+=(const u32 time)
	{
		this->m_nTime += time;
		return *this;
	}

	Timer Timer::operator-(const Timer& other) const
	{
		Timer out;
		out.m_nTime = this->m_nTime - other.m_nTime;
		return out;
	}

	Timer Timer::operator-(const u32 time) const
	{
		Timer out;
		out.m_nTime = this->m_nTime - time;
		return out;
	}

	Timer& Timer::operator-=(const Timer& other)
	{
		this->m_nTime -= other.m_nTime;
		return *this;
	}

	Timer& Timer::operator-=(const u32 time)
	{
		this->m_nTime -= time;
		return *this;
	}

	bool Timer::operator<(const Timer& other) const
	{
		return this->m_nTime < other.m_nTime;
	}

	bool Timer::operator>(const Timer& other) const
	{
		return this->m_nTime > other.m_nTime;
	}

	Timer::operator u32() const
	{
		return m_nTime;
	}

	bool Timer::isWorked()
	{
		return m_bWorked;
	}

	void Timer::_tick()
	{
		m_nTime += TimeManager::getTick();
	}

} // namespace util
} // namespace sol9